package com.huike.report.service.impl;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.*;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.Report;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;
    
    @Autowired
    private ReportMapper reportMpper;

    /**
     * 用户详细查询
     *
     * @param request
     * @param now
     * @return
     */
    @Override
    public Map<String, Object> getcontractsBasicInfo(IndexStatisticsVo request, String now) {
        HashMap<String, Object> result = new HashMap<>();
        String username = SecurityUtils.getUsername();
        Map baseData = reportMpper.getBaseData(request, now, username);
        Map todayData = reportMpper.getTodayData(request, now, username);
        Map todoData = reportMpper.getTodoData(request, now, username);
        result.putAll(baseData);
        result.putAll(todayData);
        result.putAll(todoData);
        if (!username.equals("admin")) {
            //不是admin 不能看见待分配的商机和待分配的线索
            result.put("toallocatedBusinessNum", 0);
            result.put("toallocatedCluesNum", 0);
        }
        return result;
    }

    /**
     * 线索转换龙虎榜
     * @param request
     * @return
     */
    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list= contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId= (Long) datum.get("dept_id");
            if(deptId!=null){
                SysDept dept= deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }


    @Override
    public Report getVulnerabilityMap(String beginCreateTime, String endCreateTime) {
        //创建封装时间对象,
        IndexStatisticsVo indexVo = new IndexStatisticsVo();
        //设置开始时间
        indexVo.setBeginCreateTime(beginCreateTime);
        //设置结束时间
        indexVo.setEndCreateTime(endCreateTime);
        //调用reportMpper.getVulnerabilityMap(indexVo)传入参数为封装对象
       // List<Map<String, Object>> list = reportMpper.getVulnerabilityMap(indexVo);
        List<Map<String, Object>> list=clueMapper.getVulnerabilityMap(indexVo);
        //将结果封装到数组
        int[] cluesNum = new int[8];
        for (Map<String, Object> map : list) {
            for (int i = 1; i <= 7; i++) {
                //根据键取出值,值为status对应的状态(1.2.3.4.5.6.7.8)
                if (map.get("status").equals(i + "")) {
                    //第一次循环,将状态status=1的值,所对应的统计量num(count)存入数组
                    cluesNum[i] = ((Long) map.get("num")).intValue();
                }
            }
        }

        Report report = new Report();
        //设置总线索数
        report.setCluesNums((cluesNum[1] + cluesNum[2] + cluesNum[3] + cluesNum[4] + cluesNum[6] + cluesNum[7]) + "");
        //有效线索数
        report.setEffectiveCluesNums(cluesNum[1] + cluesNum[2] + cluesNum[6] + cluesNum[7] + "");
        //商机数
        report.setBusinessNums(cluesNum[6] + cluesNum[7] + "");
        //合同数
        report.setContractNums(cluesNum[7] + "");

        return report;
    }

    private BigDecimal getRadio(Integer all, Long num) {
        if(all.intValue()==0){
            return new BigDecimal(0);
        }
		BigDecimal numBigDecimal = new BigDecimal(num);
		BigDecimal allBigDecimal = new BigDecimal(all);
		BigDecimal divide = numBigDecimal.divide(allBigDecimal,4,BigDecimal.ROUND_HALF_UP);
		return divide.multiply(new BigDecimal(100));
	}



}